import streamlit as st
from audiorecorder import audiorecorder
import speech_recognition as sr
import os

# 设置页面标题
st.title("语音识别与回复")
st.write("点击下方按钮开始录音，录音结束后可以下载音频文件并查看识别结果。")

# 初始化录音器
audio = audiorecorder("点击开始录音", "点击停止录音")

# 如果有录音数据
if len(audio) > 0:
    # 保存录音文件
    audio_path = "audio.wav"
    with open(audio_path, "wb") as f:
        f.write(audio.export().read())

    # 播放录音
    st.audio(audio.export().read(), format="audio/wav")

    # 下载录音文件
    st.write("点击下方按钮下载录音文件：")
    with open(audio_path, "rb") as f:
        st.download_button(
            label="下载录音",
            data=f,
            file_name="recording.wav",
            mime="audio/wav"
        )
